﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace App
{
    public class CB : System.Windows.Forms.ComboBox
    {
        private Color cColor = SystemColors.GradientActiveCaption;
        private Color bColor = SystemColors.GradientActiveCaption;


        public CB()
            : base()
        {
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.DrawMode = DrawMode.OwnerDrawFixed;
            this.DropDownStyle = ComboBoxStyle.DropDownList;
            this.UpdateStyles();
        }


        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            DrawCombo(e);
            base.OnDrawItem(e);
        }


        protected override void OnMouseEnter(EventArgs e)
        {
            //Graphics gr = this.CreateGraphics();
            //gr.FillRectangle(new SolidBrush(Color.FromArgb(227, 251, 157)), this.Width - this.Height + 2, 3, this.Height - 5, this.Height - 6);
            //
            //BackColor = Color.FromArgb(227, 251, 157);
            base.OnMouseEnter(e);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            //this.Invalidate(new Rectangle(this.Width - this.Height + 2, 3, this.Height - 5, this.Height - 6));
            //this.Update();
            //
            //BackColor = Color.White;
            base.OnMouseLeave(e);
        }


        private void DrawCombo(System.Windows.Forms.DrawItemEventArgs e)
        {
            Graphics g = e.Graphics;
            Rectangle rd = e.Bounds;

            int rr = this.cColor.R;
            int gg = this.cColor.G;
            int bb = this.cColor.B;
            Color cll = Color.White;
            Color cl = Color.FromArgb(rr + (255 - rr) * 2 / 3, gg + (255 - gg) * 2 / 3, bb + (255 - bb) * 2 / 3);
            Color cc = Color.FromArgb(rr + (255 - rr) * 1 / 3, gg + (255 - gg) * 1 / 3, bb + (255 - bb) * 1 / 3);
            Color cd = Color.FromArgb(rr, gg, bb);
            Color cdd = Color.FromArgb(rr * 2 / 3, gg * 2 / 3, bb * 2 / 3);

            LinearGradientBrush br = new LinearGradientBrush(new Rectangle(e.Bounds.Left - 1, e.Bounds.Top - 1, e.Bounds.Width + 4, e.Bounds.Height + 4), cd, cll, 65f);

            if (e.Index >= 0)
            {
                if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))
                {
                    Brush brs = new SolidBrush(Color.White);
                    e.Graphics.FillRectangle(brs, rd);
                    e.DrawFocusRectangle();
                    brs.Dispose();
                }
                else
                {
                    //e.Graphics.FillRectangle(br, rd);
                    //br = new LinearGradientBrush(e.Bounds, cc, cll, 65f);
                    //rd.Width = e.Bounds.Width - 2;
                    //rd.Height = e.Bounds.Height - 2;
                    //rd.X = e.Bounds.X + 1;
                    //rd.Y = e.Bounds.Y + 1;
                    Brush brs = new SolidBrush(Color.White);
                    e.Graphics.FillRectangle(brs, rd);
                    e.DrawFocusRectangle();
                    brs.Dispose();
                }
            }

            rd.Width = 20;
            rd.Height = this.ItemHeight - 5;
            rd.X = 4;
            rd.Y += 1;

            if (e.Index >= 0)
            {
                //g.FillRectangle(new SolidBrush(Color.GreenYellow), rd);
                //g.DrawRectangle(new Pen(Color.Black), rd);
                if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))
                    g.DrawString(GetItemText(this.Items[e.Index]), this.Font, new SolidBrush(Color.FromArgb(21, 66, 139)), 5, rd.Top - 1);
                else
                    g.DrawString(GetItemText(this.Items[e.Index]), this.Font, new SolidBrush(Color.FromArgb(21, 66, 139)), 5, rd.Top - 1);
            }
            //
            Graphics gr = this.CreateGraphics();
            gr.DrawRectangle(new Pen(Color.FromArgb(141, 178, 227)), 0, 0, this.Width - 1, this.Height - 1);
            gr.DrawRectangle(new Pen(Color.White), 1, 1, this.Width - 3, this.Height - 3);
            gr.FillRectangle(new SolidBrush(cll), this.Width - this.Height + 1, 2, this.Height - 3, this.Height - 4);

            br = new LinearGradientBrush(new Rectangle(this.Width - this.Height + 1, 2, this.Height, this.Height), cl, cd, 45f);
            gr.FillRectangle(br, this.Width - this.Height + 2, 3, this.Height - 5, this.Height - 6);

            br = new LinearGradientBrush(new Rectangle(this.Width - this.Height + 1, 2, this.Height, this.Height), cll, cc, 45f);
            gr.FillRectangle(br, this.Width - this.Height + 3, 4, this.Height - 7, this.Height - 8);

            Brush brss = new SolidBrush(cll);
            gr.FillRectangle(brss, this.Width - this.Height + 2, 3, 1, 1);
            gr.FillRectangle(brss, this.Width - this.Height + 2, 3 + this.Height - 7, 1, 1);
            gr.FillRectangle(brss, this.Width - this.Height + 2 + this.Height - 6, 3, 1, 1);
            brss = new SolidBrush(cl);
            gr.FillRectangle(new SolidBrush(cl), this.Width - this.Height + 2 + this.Height - 6, 3 + this.Height - 7, 1, 1);

            gr.DrawLine(new Pen(cd, 1), (this.Width - this.Height / 2) - 4, this.Height / 2 - 1, (this.Width - this.Height / 2) - 1, this.Height / 2 + 2);
            gr.DrawLine(new Pen(cd, 1), (this.Width - this.Height / 2) - 1, this.Height / 2 + 2, (this.Width - this.Height / 2) + 2, this.Height / 2 - 1);
            gr.DrawLine(new Pen(cc, 1), (this.Width - this.Height / 2) - 4, this.Height / 2 - 0, (this.Width - this.Height / 2) - 1, this.Height / 2 + 3);
            gr.DrawLine(new Pen(cc, 1), (this.Width - this.Height / 2) - 1, this.Height / 2 + 3, (this.Width - this.Height / 2) + 2, this.Height / 2 - 0);
            gr.DrawLine(new Pen(cl, 1), (this.Width - this.Height / 2) - 4, this.Height / 2 + 1, (this.Width - this.Height / 2) - 1, this.Height / 2 + 4);
            gr.DrawLine(new Pen(cl, 1), (this.Width - this.Height / 2) - 1, this.Height / 2 + 4, (this.Width - this.Height / 2) + 2, this.Height / 2 + 1);

            //
            brss.Dispose();
            br.Dispose();
        }
    }
}
